package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.ILegalSensitiveWordService;
import com.mdd.admin.validate.LegalSensitiveWordCreateValidate;
import com.mdd.admin.validate.LegalSensitiveWordSearchValidate;
import com.mdd.admin.validate.LegalSensitiveWordUpdateValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.LegalSensitiveWordDetailVo;
import com.mdd.admin.vo.LegalSensitiveWordListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.LegalSensitiveWord;
import com.mdd.common.mapper.LegalSensitiveWordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 敏感词列实现类
 * @author LikeAdmin
 */
@Service
public class LegalSensitiveWordServiceImpl implements ILegalSensitiveWordService {
        
    @Resource
    LegalSensitiveWordMapper legalSensitiveWordMapper;

    /**
     * 敏感词列列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<LegalSensitiveWordListedVo>
     */
    @Override
    public PageResult<LegalSensitiveWordListedVo> list(PageValidate pageValidate, LegalSensitiveWordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<LegalSensitiveWord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        legalSensitiveWordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:code:str",
            "like:tplName@tpl_name:str",
            "=:word:str",
        });

        IPage<LegalSensitiveWord> iPage = legalSensitiveWordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<LegalSensitiveWordListedVo> list = new LinkedList<>();
        for(LegalSensitiveWord item : iPage.getRecords()) {
            LegalSensitiveWordListedVo vo = new LegalSensitiveWordListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 敏感词列详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return LegalSensitiveWord
     */
    @Override
    public LegalSensitiveWordDetailVo detail(Integer id) {
        LegalSensitiveWord model = legalSensitiveWordMapper.selectOne(
                new QueryWrapper<LegalSensitiveWord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        LegalSensitiveWordDetailVo vo = new LegalSensitiveWordDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 敏感词列新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(LegalSensitiveWordCreateValidate createValidate) {
        LegalSensitiveWord model = new LegalSensitiveWord();
        model.setCode(createValidate.getCode());
        model.setTplName(createValidate.getTplName());
        model.setWord(createValidate.getWord());
        legalSensitiveWordMapper.insert(model);
    }

    /**
     * 敏感词列编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(LegalSensitiveWordUpdateValidate updateValidate) {
        LegalSensitiveWord model = legalSensitiveWordMapper.selectOne(
                new QueryWrapper<LegalSensitiveWord>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCode(updateValidate.getCode());
        model.setTplName(updateValidate.getTplName());
        model.setWord(updateValidate.getWord());
        legalSensitiveWordMapper.updateById(model);
    }

    /**
     * 敏感词列删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        LegalSensitiveWord model = legalSensitiveWordMapper.selectOne(
                new QueryWrapper<LegalSensitiveWord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        legalSensitiveWordMapper.delete(new QueryWrapper<LegalSensitiveWord>().eq("id", id));
    }

}
